package system

import (
	"context"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/system"
	systemReq "github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
)

type Certificate_managementService struct{}

// CreateCertificate_management 创建证书管理记录
// Author [yourname](https://github.com/yourname)
func (certificate_managementService *Certificate_managementService) CreateCertificate_management(ctx context.Context, certificate_management *system.Certificate_management) (err error) {
	err = global.GVA_DB.Create(certificate_management).Error
	return err
}

// DeleteCertificate_management 删除证书管理记录
// Author [yourname](https://github.com/yourname)
func (certificate_managementService *Certificate_managementService) DeleteCertificate_management(ctx context.Context, ID string) (err error) {
	err = global.GVA_DB.Delete(&system.Certificate_management{}, "id = ?", ID).Error
	return err
}

// DeleteCertificate_managementByIds 批量删除证书管理记录
// Author [yourname](https://github.com/yourname)
func (certificate_managementService *Certificate_managementService) DeleteCertificate_managementByIds(ctx context.Context, IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]system.Certificate_management{}, "id in ?", IDs).Error
	return err
}

// UpdateCertificate_management 更新证书管理记录
// Author [yourname](https://github.com/yourname)
func (certificate_managementService *Certificate_managementService) UpdateCertificate_management(ctx context.Context, certificate_management system.Certificate_management) (err error) {
	err = global.GVA_DB.Model(&system.Certificate_management{}).Where("id = ?", certificate_management.ID).Updates(&certificate_management).Error
	return err
}

// GetCertificate_management 根据ID获取证书管理记录
// Author [yourname](https://github.com/yourname)
func (certificate_managementService *Certificate_managementService) GetCertificate_management(ctx context.Context, ID string) (certificate_management system.Certificate_management, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&certificate_management).Error
	return
}

// GetCertificate_managementInfoList 分页获取证书管理记录
// Author [yourname](https://github.com/yourname)
func (certificate_managementService *Certificate_managementService) GetCertificate_managementInfoList(ctx context.Context, info systemReq.Certificate_managementSearch) (list []system.Certificate_management, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&system.Certificate_management{})
	var certificate_managements []system.Certificate_management
	// 如果有条件搜索 下方会自动创建搜索语句
	if len(info.CreatedAtRange) == 2 {
		db = db.Where("created_at BETWEEN ? AND ?", info.CreatedAtRange[0], info.CreatedAtRange[1])
	}

	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&certificate_managements).Error
	return certificate_managements, total, err
}
func (certificate_managementService *Certificate_managementService) GetCertificate_managementPublic(ctx context.Context) {
	// 此方法为获取数据源定义的数据
	// 请自行实现
}
